using System;
using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using DragonBones;
using UnityEngine;
using UnityEngine.UI;

public class LevelFailure : MonoBehaviour
{
    [SerializeField]
    private bool autoUpdate;

    [SerializeField]
    private bool animationEnabled;

    [SerializeField]
    private float mainScaleSpeed;

    [SerializeField]
    private float backScaleSpeed;

    [SerializeField]
    private float backMaxScale;

    [SerializeField]
    private float backAlphaDecaySpeed;

    [SerializeField]
    private Image back;

    [SerializeField]
    private Image main;
    [SerializeField]
    private UnityArmatureComponent unityArmature;
    public Text text;
    public Image mask;
    public Action step1;
    public Action step2;
    public void ShowPanel()
    {
        back.gameObject.SetActive(true);
        main.gameObject.SetActive(true);
        text.text = "第" + (Main.Instance.saveData.LevelIndex + 1) + "关";
        Sequence sequence = DOTween.Sequence();
        sequence.Append(back.transform.DOScale(Vector3.one * 1.00f, 0.30f));
        sequence.Join(back.DOFade(1f, 0.1f).SetEase(Ease.InQuad));
        sequence.Append(back.transform.DOScale(Vector3.one * 0.9f, 0.30f));
        sequence.Join(back.DOFade(0f, 0.1f).SetEase(Ease.OutQuad));
        //sequence.AppendInterval(1f);
        sequence.AppendCallback(() =>
        {
            StartCoroutine(NextStep());
        });
        // sequence.AppendCallback(() => step2());
        // sequence.Append(text.transform.DOMoveY(text.transform.position.y - 400f, 1.5f).SetEase(Ease.OutQuad));
        // sequence.Append(text.transform.DOMoveY(text.transform.position.y, 1.5f).SetEase(Ease.InQuad));
        // sequence.OnComplete(() =>
        // {
        //     gameObject.SetActive(false);
        //     Main.Instance.gameStatus = GameStatus.Playing;
        // });
        // text.text = "第" + (Main.Instance.saveData.LevelIndex + 1) + "关";
        // unityArmature.gameObject.SetActive(true);
        // unityArmature.animation.Play("newAnimation", 1);
        // unityArmature.AddDBEventListener(EventObject.COMPLETE, OnAnimationComplete);

    }
    private void OnAnimationComplete(string type, EventObject eventObject)
    {
        if (eventObject.animationState.name == "newAnimation")
        {
            StartCoroutine(NextStep());
        }
    }
    IEnumerator NextStep()
    {
        yield return new WaitForSeconds(1f);
        //unityArmature.gameObject.SetActive(false);
        back.gameObject.SetActive(false);
        main.gameObject.SetActive(false);
        step1();
        step2();
        Sequence sequence = DOTween.Sequence();
        sequence.Append(text.transform.DOMoveY(text.transform.position.y - (Screen.height / 2), 2.5f).SetEase(Ease.OutQuad));
        sequence.AppendInterval(1f);
        sequence.Append(text.transform.DOMoveY(text.transform.position.y, 2f).SetEase(Ease.InQuad));
        sequence.OnComplete(() =>
        {
            gameObject.SetActive(false);
            Main.Instance.gameStatus = GameStatus.Playing;
            Main.Instance.level.ResetForce();
            AdManager.Instance.ShowNative(0f);
        });
    }
}
